<template>
  <el-container class="home-container">
    <!--  头部区域  -->
    <el-header>
      <div class="header_left">
        <img src="@/assets/images/moon.png" alt="">
        <span>电商后台管理系统</span>
      </div>

      <div class="header_right">
        <el-button type="info" @click="signOut">退出</el-button>
      </div>
    </el-header>
    <!--  页面主题区  -->
    <el-container>
      <!--   侧边栏   -->
      <el-aside :width="asideWidth + 'px'">
        <div class="changeCollapse" @click="changeCollapse">|||</div>
        <el-menu background-color="#334154" :collapse="collapse"
                 text-color="#fff" active-text-color="#5A9CF8"
                 :default-active="$route.path.startsWith('/')?$route.path.slice(1):$route.path"
                 :collapse-transition="false" router unique-opened>
          <!--    一级菜单      -->
          <el-submenu :show-timeout="showTimeout" v-for="item in menuList" :key="item.id" :index="item.id.toString()">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span slot="title">{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item v-for="child in item.children" :key="child.id" :index="child.path">
              <i class="el-icon-menu"></i>
              <span slot="title">{{ child.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主题 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      showTimeout: 50,
      // 侧边栏宽度
      asideWidth: 200,
      // 是否水平收起菜单
      collapse: false,
      // 左侧菜单数据
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },
  // 在页面加载之后，创建获取菜单列表
  created () {
    return this.getMenuList()
  },
  methods: {
    changeCollapse () {
      this.collapse = !this.collapse
      this.asideWidth = this.collapse ? 64 : 200
    },
    signOut () {
      this.config.authorization.token = null
      this.$router.push('/login')
    },
    // 获取菜单列表
    async getMenuList () {
      const { data: res } = await this.http.get('/menus')
      if (res.meta.status !== 200) {
        await this.$router.push('/login')
        return this.$message.error(`${res.meta.status} ${res.meta.msg}`)
      }
      this.menuList = res.data
    }
  }
}
</script>

<style lang="less" scoped>

.el-container {
  height: 100%;
}

.el-header {
  height: 45px !important;
  padding-left: 5px;
  background-color: #373D41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #ccc;
  font-size: 18px;

  .header_left {
    height: 100%;
    padding: 0;

    img {
      box-sizing: border-box;
      height: 80%;
      border-radius: 30px;
    }
  }

  > div {
    display: flex;
    align-items: center;
  }

  span {
    margin-left: 15px;
  }
}

.el-aside {
  background-color: #373D41;

  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.el-menu-item {
  background-color: #222D3C !important;
}

.el-menu-item:hover {
  background-color: #041527 !important;
}

.iconfont {
  margin-right: 10px;
}

.changeCollapse {
  background-color: #4A5064;
  text-align: center;
  letter-spacing: 0.2em;
  color: #fff;
}

.changeCollapse:hover {
  cursor: pointer;
}
</style>
